package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.annotation.AutoFill;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetMealDishMapper setMealDishMapper;

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

    /**
     * 新增菜品
     *
     * @param dto
     */
    @Transactional
    @Override
    public void addDish(DishDTO dto) {
        Dish dish = new Dish();
        //拷贝数据
        BeanUtils.copyProperties(dto,dish);
        //存储菜品
        dishMapper.addDish(dish);
        //补充风味信息
        List<DishFlavor> flavors = dto.getFlavors();
        flavors.forEach(flavor -> {
            flavor.setDishId(dish.getId());
        });
        if(!flavors.isEmpty()){
            //存储风味
            dishFlavorMapper.addDishFlavor(flavors);
        }
    }

    /**
     * 菜品分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public PageResult pagesGetDish(DishPageQueryDTO dto) {
        PageHelper.startPage(dto.getPage(),dto.getPageSize());
        Page<DishVO> dishes = dishMapper.pagesGetDish(dto);
        return new PageResult(dishes.getTotal(),dishes.getResult());
    }

    /**
     * 删除菜品
     *
     * @param ids
     */
    @Override
    @Transactional
    public void deleteDish(List<Long> ids) {
        //判断菜品是否能被删除
        for (Long id : ids) {
            //判断菜品状态,起售中不能删除
            Dish dish = dishMapper.getById(id);
            if(dish.getStatus().equals(StatusConstant.ENABLE)){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
            //判断是否关联了套餐
            Integer count = setMealDishMapper.countByDish(id);
            if(count > 0){
                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }
        }
        //删除菜品
        dishMapper.deleteDish(ids);
        //删除风味
        dishFlavorMapper.deleteDish(ids);
    }

    /**
     * 根据id查询菜品
     *
     * @param id
     * @return
     */
    @Override
    public DishVO getByID(Long id) {
        DishVO dishVO = new DishVO();
        //根据id查询菜品
        Dish dish = dishMapper.getById(id);
        BeanUtils.copyProperties(dish,dishVO);
        //根据id查询口味
        List<DishFlavor> dishFlavors = dishFlavorMapper.getByDishId(id);
        dishVO.setFlavors(dishFlavors);
        return dishVO;
    }

    /**
     * 修改菜品
     *
     * @param dto
     */
    @Override
    @Transactional
    public void modifyDish(DishDTO dto) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dto,dish);
        //更新菜品
        dishMapper.modifyDish(dish);
        //删除并新增菜品口味
        List<Long> ids = new ArrayList<>();
        ids.add(dto.getId());
        dishFlavorMapper.deleteDish(ids);
        if(dto.getFlavors()!=null && !dto.getFlavors().isEmpty()){
            dto.getFlavors().forEach(flavor -> {
                flavor.setDishId(dto.getId());
            });
            dishFlavorMapper.addDishFlavor(dto.getFlavors());
        }
    }

    /**
     * 菜品起售、停售
     *
     * @param status
     * @param id
     */
    @Override
    public void dishStatus(Integer status, Long id) {
        Dish dish = Dish.builder()
                .status(status)
                .id(id)
                .build();
        dishMapper.modifyDish(dish);
    }

    /**
     * 根据分类id查询菜品
     *
     * @param id
     * @return
     */
    @Override
    public List<DishVO> getByCate(Long id) {
        return dishMapper.getByCate(id);
    }
}
